﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.SharePoint.Commands;
using SPVSX.Explorer.Extensions;
using Microsoft.SharePoint;
using SPVSX.Explorer.Extensions.NodeInformation;
using SPVSX.Explorer.Extensions.CommandIds;

namespace SPVSX.Explorer.Commands
{
    /// <summary>
    /// The web part gallery commands
    /// </summary>
    class WebPartGalleryCommands
    {
        /// <summary>
        /// Gets data for each Web Part on the SharePoint site, and returns an array of 
        /// serializable objects that contain the data.
        /// </summary>
        /// <param name="context">The command context</param>
        /// <returns>The webpart infos</returns>
        [SharePointCommand(WebPartGalleryCommandIds.GetWebParts)]
        private static WebPartNodeInfo[] GetWebParts(ISharePointCommandContext context)
        {
            var nodeInfos = new List<WebPartNodeInfo>();
            SPListItemCollection webParts = context.Site.GetCatalog(
                SPListTemplateType.WebPartCatalog).Items;

            foreach (SPListItem webPart in webParts)
            {
                WebPartNodeInfo nodeInfo = new WebPartNodeInfo
                {
                    Id = webPart.ID,
                    SiteId = webPart.ParentList.ParentWeb.ID,
                    Name = webPart.Title,
                    UniqueId = webPart.UniqueId,
                    ImageUrl = webPart.ParentList.ImageUrl,
                    Url = webPart.Url
                };
                nodeInfos.Add(nodeInfo);
            }

            return nodeInfos.ToArray();
        }

        /// <summary>
        /// Gets data for each Web Part on the SharePoint site, and returns an array of 
        /// serializable objects that contain the data.
        /// </summary>
        /// <param name="context">The command context</param>
        /// <returns>The webpart infos</returns>
        [SharePointCommand(WebPartGalleryCommandIds.GetWebPartGallery)]
        private static WebPartGalleryNodeInfo GetWebPartGallery(ISharePointCommandContext context)
        {
            SPList webPartGallery = context.Site.GetCatalog(
                SPListTemplateType.WebPartCatalog);

            WebPartGalleryNodeInfo nodeInfo = new WebPartGalleryNodeInfo
            {
                Id = webPartGallery.ID,
                SiteId = webPartGallery.ParentWeb.ID,
                Name = webPartGallery.Title,
                Url = webPartGallery.DefaultViewUrl
            };

            return nodeInfo;
        }

        /// <summary>
        /// Gets additional property data for a specific Web Part.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="nodeInfo"></param>
        /// <returns></returns>
        [SharePointCommand(WebPartGalleryCommandIds.GetWebPartGalleryProperties)]
        private static Dictionary<string, string> GetWebPartGalleryProperties(ISharePointCommandContext context,
            WebPartGalleryNodeInfo nodeInfo)
        {
            return SharePointCommandServices.GetProperties(context.Site.GetCatalog(SPListTemplateType.WebPartCatalog));
        }

        /// <summary>
        /// Get the default view url for the web part gallery
        /// </summary>
        /// <param name="context">The context</param>
        /// <returns>The default view of the web part gallery</returns>
        [SharePointCommand(WebPartGalleryCommandIds.GetWebPartGalleryAllItemsUrl)]
        private static string GetWebPartGalleryAllItemsUrl(ISharePointCommandContext context)
        {
            return context.Site.GetCatalog(SPListTemplateType.WebPartCatalog).DefaultViewUrl;
        }
    }
}
